﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace StackAttack_WindowsFormApp.PlayersData
{
    class LeaderboardManager
    {

        private string fullPathToXmlFile;
        public bool IsFirstInitialize { get; set; }

        private XDocument doc;
        
        public LeaderboardManager()
        {
            string fullPathToAssembly = Assembly.GetExecutingAssembly().Location;
            string dir = Path.GetDirectoryName(fullPathToAssembly);
            fullPathToXmlFile = Path.Combine(dir, "leaderBoard.xml");

            IfFileExist();
        }

        public void AddPlayerToFile(string name, int score)
        {
            if (IsFirstInitialize)
            {
                XElement element = new XElement(new XElement("players", new XElement("player",
                new XElement("name", name),
                new XElement("score", score))));
                   doc = new XDocument(element);

                   doc.Save(fullPathToXmlFile);
            }
            else
            {
                doc = XDocument.Load(fullPathToXmlFile);
                foreach (XElement item in doc.Root.Elements())
                {
                    int c = Int32.Parse(item.Element("score").Value);
                    if (Int32.Parse(item.Element("score").Value) < score)
                    {
                        //if (item == doc.FirstNode)
                        //{
                        //    doc.Root.AddFirst(new XElement("player",
                        //   new XElement("name", name),
                        //   new XElement("score", score)));
                      
                        //}
                        //else
                        //{
                            item.AddBeforeSelf(new XElement("player",
                           new XElement("name", name),
                           new XElement("score", score)));
                        //}
                        doc.Save(fullPathToXmlFile);
                        return;

                    }
                }
                XElement element = new XElement("player",
                   new XElement("name", name),
                   new XElement("score", score));
                doc.Root.Add(element);
                doc.Save(fullPathToXmlFile);
            }
        }

        public List<string> GetTopFivePlayers()
        {
            List<string> namesOfTopFivePlayers = new List<string>();
            if (IsFirstInitialize)
            {
                namesOfTopFivePlayers.Add("No one play this game yet!");
                return namesOfTopFivePlayers;
            }

            doc = XDocument.Load(fullPathToXmlFile);
            var realQuantityOfPlayers = doc.Root.Elements().Count();
            int quantityOfPlayers;

            if (realQuantityOfPlayers > 5)
            {
                quantityOfPlayers = 5;
            }
            else
            {
                quantityOfPlayers = realQuantityOfPlayers;
            }
            

            foreach (var item in doc.Root.Elements())
            {
                if (quantityOfPlayers == 0)
                {
                    break;
                }
                namesOfTopFivePlayers.Add(string.Format("{0} : {1}", item.Element("name").Value, item.Element("score").Value));
                quantityOfPlayers--;
            }

            return namesOfTopFivePlayers;

        }


        private void IfFileExist()
        {
            if (!File.Exists(fullPathToXmlFile))
            {
                IsFirstInitialize = true;
            }

        }
    }
}
